<?php
/**
 * Created by glq.
 * Date: 2018/12/21
 * Time: 13:00
 */

namespace Slideshow\Controller;

use Common\Controller\AdminbaseController;
use Label\Model\CategoryModel;
use Portal\Model\PostsModel;

class AdminslideshowController extends AdminbaseController{

    protected $posts_model;

    function _initialize() {
        parent::_initialize();
        $this->posts_model = D("Portal/Posts");
    }
    
    // 轮播列表
    public function slide_show_list()
    {
        $this->_lists(array("p.post_status" => array('neq', PostsModel::POST_STATUS_DEL), 'p.post_type' => PostsModel::POST_TYPE_SLIDESHOW));
        $this->display();
    }

    // 添加轮播
    public function add(){
        $cat_model = new CategoryModel();
        $this->assign('slide_show_positions', $cat_model->slide_show_positions);
        $this->display();
    }

    // 添加轮播提交
    public function add_slide_show(){
        if (IS_POST) {
            $category = $_POST['post']['category'];
            if (empty($category)) {
                $this->error("请至少选择一个分类！");
            }
            $_POST['post']['category'] = implode(',', $category);

            $_POST['post']['post_modified'] = date("Y-m-d H:i:s",time());
            $_POST['post']['post_type']     = PostsModel::POST_TYPE_SLIDESHOW;
            $_POST['smeta']['pc_thumb']     = sp_asset_relative_url($_POST['smeta']['pc_thumb']);
            $_POST['smeta']['mobile_thumb'] = sp_asset_relative_url($_POST['smeta']['mobile_thumb']);

            $slide_show = I("post.post");
            $slide_show['smeta'] = json_encode($_POST['smeta']);
            $result=$this->posts_model->add($slide_show);
            if ($result) {
                $this->success("添加成功！");
            } else {
                $this->error("添加失败！");
            }
        }
    }


    // 编辑轮播
    public function edit_slide_show()
    {
        $id   = I('get.id', 0, 'intval');
        $post = $this->posts_model
            ->alias('p')
            ->where("id = $id")
            ->find();

        // 轮播位置
        $cat_model = new CategoryModel();
        $this->assign('slide_show_positions', $cat_model->slide_show_positions);

        $this->assign("post",$post);
        $this->assign("smeta",json_decode($post['smeta'],true));
        $this->display();
    }

    // 修改轮播提交
    public function update_slide_show()
    {
        if (IS_POST) {

            $category = $_POST['post']['category'];
            if (empty($category)) {
                $this->error("请至少选择一个分类！");
            }
            $_POST['post']['category']          = implode(',', $category);
            $_POST['post']['post_modified']     = date("Y-m-d H:i:s",time());
            $_POST['smeta']['pc_thumb']         = sp_asset_relative_url($_POST['smeta']['pc_thumb']);
            $_POST['smeta']['mobile_thumb']     = sp_asset_relative_url($_POST['smeta']['mobile_thumb']);

            $slide_show = I("post.post");
            $slide_show['smeta'] = json_encode($_POST['smeta']);
            $result = $this->posts_model->save($slide_show);
            if ($result !== false) {
                $this->success("保存成功！");
            } else {
                $this->error("保存失败！");
            }
        }
    }

    // 删除轮播
    public function del_slide_show()
    {
        $post_id = I('get.id', 0, 'intval');
        $this->posts_model->where(['id' => $post_id])->save(['post_status' => PostsModel::POST_STATUS_DEL]);
        $this->success('删除成功');
    }

    private function _lists($where = array()) {

        // 开始时间
        $position = I('request.position');
        if(!empty($position)){
            $where[] = 'FIND_IN_SET(' . $position . ', p.category)';
        }

        // 开始时间
        $start_time = I('request.start_time');
        if(!empty($start_time)){
            $where['p.post_date'] = array(
                array('EGT',$start_time)
            );
        }

        // 结束时间
        $end_time=I('request.end_time');
        if(!empty($end_time)){
            if(empty($where['p.post_date'])){
                $where['p.post_date']=array();
            }
            array_push($where['p.post_date'], array('ELT',$end_time));
        }

        // 标题模糊搜索
        $keyword=I('request.keyword');
        if(!empty($keyword)) {
            $where['p.post_title']=array('like',"%$keyword%");
        }

        $count = $this->posts_model
            ->alias("p")
            ->where($where)
            ->count();
        $page = $this->page($count, 20);

        // 轮播位置
        $cat_model = new CategoryModel();
        $this->assign('slide_show_positions', $cat_model->slide_show_positions);

        $this->posts_model
            ->alias("p")
            ->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->order("p.post_order desc, p.post_date DESC");

        $posts = $this->posts_model->select();

        $this->assign("page", $page->show('Admin'));
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);
    }
}